﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;
using Touch2Like.DAL;
using System.Net.Mail;
using System.Net;

namespace Touch2Like.Web.Customer
{
    public partial class signup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Signup_Click(object sender, EventArgs e)
        {
            if (tb_FirstName.Text.Trim() == "")
            {
                lb_Msg.Text = "Firstname is required.";
                return;
            }
            if (tb_LastName.Text.Trim() == "")
            {
                lb_Msg.Text = "Lastname is required.";
                return;
            }
            if (tb_Username.Text.Trim() == "")
            {
                lb_Msg.Text = "Username is required.";
                return;
            }
            if (tb_Email.Text.Trim() == "")
            {
                lb_Msg.Text = "Email is required.";
                return;
            }
            if (tb_Password.Text.Trim() == "")
            {
                lb_Msg.Text = "Password is required.";
                return;
            }
            if (tb_Password.Text.Trim() != tb_Password1.Text.Trim())
            {
                lb_Msg.Text = "Error input.";
                return;
            }

            UserDAL user = new UserDAL();
            //if (AuthorizedUserDAL.AuthorizedUserVerify(tb_Email.Text.Trim(), tb_AcCode.Text.Trim()) == 0)
            //{
            //    lb_Msg.Text = "Authorization failed, please try again.";
            //    return;
            //}
            if (user.GetUserByNameAndEmail(0, tb_Username.Text.Trim()) != null)
            {
                lb_Msg.Text = "Username already exists, please try another one.";
                return;
            }
            if (tb_Email.Text.Trim() != "" && user.GetUserByNameAndEmail(1, tb_Email.Text.Trim()) != null)
            {
                lb_Msg.Text = "Email already exists, please try another one.";
                return;
            }
            UserInfo userInfo = new UserInfo();
            userInfo.FirstName = tb_FirstName.Text.Trim();
            userInfo.LastName = tb_LastName.Text.Trim();
            userInfo.UserName = tb_Username.Text.Trim();
            userInfo.Email = tb_Email.Text.Trim();
            userInfo.Password = Utility.Encryption.Encrypt(tb_Password.Text.Trim());
            int code = 0;
            if (Int32.TryParse(tb_AcCode.Text.ToString().Trim(), out code))
                userInfo.RegistrationCode = code;
            userInfo.UserGuid = Guid.NewGuid().ToString();
            WebUtility.UserGUID = userInfo.UserGuid.ToString();

            user.UpdateUser(userInfo, "ADD");

            WebUtility.SendEmail(tb_Email.Text.Trim(), tb_Username.Text.Trim(), 0);

            PH_Signup.Visible = false;

            if (!string.IsNullOrEmpty(Request["checkout"]) && Request["checkout"] == "true")
            {
                PH_SuccessToOrder.Visible = true;
            }
            else
            {
                PH_Success.Visible = true;
            }

            //userInfo.UserName = tb_Username.Text.Trim();
            //userInfo.Email = tb_Email.Text.Trim();
            //userInfo.Password = Utility.Encryption.Encrypt(tb_Password.Text.Trim());

            //userInfo.UserGuid = Guid.NewGuid().ToString();
            //WebUtility.UserGUID = userInfo.UserGuid.ToString();

            //user.UpdateUser(userInfo, "ADD");

            //WebUtility.SendEmail(tb_Email.Text.Trim(), tb_Username.Text.Trim(), 0);

            //PH_Signup.Visible = false;

            //if (!string.IsNullOrEmpty(Request["checkout"]) && Request["checkout"] == "true")
            //{
            //    PH_SuccessToOrder.Visible = true;
            //}
            //else
            //{
            //    PH_Success.Visible = true;
            //}

            /*
            if (!string.IsNullOrEmpty(Request["next"]))
            {
                Response.Redirect(Server.UrlDecode(Request["next"]));
            }
            else
            {
                Response.Redirect("/goods/Cart.aspx");
            }*/
        }
    }
}